package com.rtp2p.rtcoresdkdemo.add.binding

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.rtp2p.rtcoresdkdemo.base.BaseFragment
import com.rtp2p.rtcoresdkdemo.databinding.AddBindingFragmentBinding
import com.rtp2p.rtp2pcore.core.BindingDevice
import com.rtp2p.rtp2pcore.utils.RTLogUtils

class AddBindingFragment : BaseFragment() {
    private val bindingDevice = BindingDevice()
    override fun initWorkspaceAction(inflater: LayoutInflater, container: ViewGroup?): View {
        val binding = AddBindingFragmentBinding.inflate(layoutInflater)

        binding.btnReturn.setOnClickListener { finish() }
        binding.btnBinding.setOnClickListener {
            val deviceId = binding.deviceId.text.toString()
            val devicePwd = binding.devicePwd.text.toString()
            val deviceName = binding.deviceName.text.toString()
            bindingDevice(deviceId, devicePwd, deviceName)
        }
        return binding.root
    }

    //D0G003-012185-LRBMM
    private fun bindingDevice(deviceId: String, devicePwd: String, deviceName: String) {
        RTLogUtils.d("deviceId:$deviceId  devicePwd:$devicePwd  deviceName:$deviceName")
        Toast.makeText(requireContext(), "正在绑定", Toast.LENGTH_SHORT).show()
        bindingDevice.binding(requireContext(), deviceId, deviceName, devicePwd) {
            if (it.success) {
                Toast.makeText(requireContext(), "$deviceId 绑定成功", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                Toast.makeText(
                    requireContext(),
                    "绑定错误 code:${it.code}  message:${it.message} ",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
}